package com.erd.datacollect.utils;

import org.springframework.lang.NonNull;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTool {
  public static Calendar getNowCalendar(){
    Calendar cal = Calendar.getInstance();
    return cal;
  }
  public static int getDateParam(@NonNull Date d, int type){
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal.get(type);
  }
  public static int getDate(@NonNull Date d){
    return getDateParam(d,Calendar.DATE);
  }
  public static int getMonth(@NonNull Date d){
    return getDateParam(d,Calendar.MONTH);
  }
  public static int getYear(@NonNull Date d){
    return getDateParam(d,Calendar.YEAR);
  }
  public static int getHour(@NonNull Date d){
    return getDateParam(d,Calendar.HOUR_OF_DAY);
  }
  public static Date getLastDate(Date d,int calendarType,int subNum){
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    int num = cal.get(calendarType);
    cal.set(calendarType,num+subNum);
    return cal.getTime();
  }
  public static Date getNow(){
    Calendar cal = Calendar.getInstance();
    return cal.getTime();
  }
  public static Calendar startHour(int hour, Calendar cal){
    cal.set(Calendar.MINUTE,0);
    cal.set(Calendar.SECOND,0);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    return cal;
  }
  //获取给定小时的第59分59秒
  public static Calendar endHour(int hour, Calendar cal){
    cal.set(Calendar.MINUTE,59);
    cal.set(Calendar.SECOND,59);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    return cal;
  }
  //获取该时间某个小时的整点
  public static Calendar startHour(int hour, Date date){
    Calendar cal=Calendar.getInstance();
    cal.setTime(date);
    return startHour(hour,cal);
  }
  public static Calendar endHour(int hour, Date date){
    Calendar cal=Calendar.getInstance();
    cal.setTime(date);
    return endHour(hour,cal);
  }
  //获取某个时间的某一天的00点
  public static Calendar startDay(int day,Calendar cal){
    startHour(0,cal);
    cal.set(Calendar.DAY_OF_MONTH,day);
    return cal;
  }
  //获取某个时间的某一天的23小时59分59秒
  public static Calendar endDay(int day,Calendar cal){
    endHour(23,cal);
    cal.set(Calendar.DAY_OF_MONTH,day);
    return cal;
  }
  public static Calendar startMonth(int month,Calendar cal){
    startDay(0,cal);
    cal.set(Calendar.MONTH,month);
    return cal;
  }
  public static Calendar addCalendar(int field,int num, Calendar cal){
    cal.add(field,num);
    return cal;
  }
  public static String dateToStr(Date d){
    return dateToStr(d,null);
  }
  public static String dateToStr(Date d,String formatStr){
    if(formatStr==null){
      formatStr="yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    }
    if(d==null){
      return "";
    }
    DateFormat format = new SimpleDateFormat(formatStr);
    return format.format(d);
  }
  public static Calendar strToCalendar(String str,String formatStr){
    Date d = strToDate(str,formatStr);
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal;
  }
  public static Date strToDate(String str){
    return strToDate(str,null);
  }
  public static Date strToDate(String str,String formatStr){
    if(formatStr==null){
      formatStr="yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    }
    DateFormat format = new SimpleDateFormat(formatStr);
    try {
      return format.parse(str);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }
  private static Date addDate(int field,int num, Calendar cal){
    return addCalendar(field,num,cal).getTime();
  }
  public static void main(String[] args){
    String str = "2023-05-08T15:29:00.206+08:00";
    int index = str.lastIndexOf(":");
    String i = str.substring(0,index);
      Date d = strToDate(i+"00");
  }
}
